/**
 * Assignment 4               P4Rational.java                  Due July 13, 2011
 *
 * login: cs11ubn
 */

// Import for toDoubleString
import java.text.DecimalFormat;

/**
 * Rational object, has numerator and denominator.
 */
public class P4Rational
{
  // Declare instance variables
  private int numerator, denominator; // Rational number
  
  /**
   * No arg ctor.
   * Initialize numerator to zero and denominator to 1
   */
  public P4Rational()
  {
    numerator = 0;
    denominator = 1;
  }
  
  /**
   * 1 arg int ctor
   * Initialize numerator to parameter
   */
  public P4Rational(int n)
  {
    numerator = n;
    denominator = 1;
  }
  
  /**
   * 2 arg int ctor
   * Initialize numerator and denominator to parameters
   * If given numerator is 0, stores denominator of 1
   * Call " reduce(); "
   */
  public P4Rational(int n, int d)
  {
    numerator = n;
    denominator = d;
    
    // Reduce the fraction
    reduce();
  }
  
  /**
   * Return a P4Rational reference with sum of this and right
   */
  public P4Rational add(P4Rational right)
  {
    return new P4Rational((numerator*right.denominator) + 
        (right.numerator*denominator), (denominator*right.denominator));
  }
  
  /**
   * Return a P4Rational reference with difference of this minus right
   */
  public P4Rational sub(P4Rational right)
  {
    return new P4Rational((numerator*right.denominator) - 
        (right.numerator*denominator), (denominator*right.denominator));
  }
  
  /**
   * Return a P4Rational reference with result of this divided by right
   */
  public P4Rational div(P4Rational right)
  {
    return new P4Rational(numerator * right.denominator, 
        right.numerator * denominator);
  }
  
  /**
   * Return a P4Rational reference with product of this and right
   * SEE given code on page 10
   */
  public P4Rational mult(P4Rational right)
  {
    return new P4Rational(numerator*right.numerator, 
        denominator*right.denominator);
  }
  
  /**
   * Calculates greatest common divisor on numerator and denominator
   * Reduces fraction by gcd only if gcd is NON-zero
   * SEE given code on page 10
   */
  public void reduce()
  {
    int gcd = 0;
    int smaller = Math.min(numerator,denominator);
    
    // Handle any negative numbers and put the sign in the numerator
    if (numerator * denominator < 0) // Check for negative
    {
      // Negative number, store sign in numerator
      numerator = - (Math.abs(numerator));
      denominator = Math.abs(denominator); // Make the denominator positive
    }
    else
    {
      // Either both are negative or neither. Store the absolute value
      numerator = Math.abs(numerator);
      denominator = Math.abs(denominator);
    }
    
    // Find the gcd, start with 2 as everything is divisable by 1
    for (int i = 2; i <= smaller; i++)
    {
      if (numerator % i == 0 && denominator % i == 0)
      {
        gcd = i; // Set the gcd
      }
    }
    
    // Check if a gcd was found, if it was then store the new values
    if (gcd != 0)
    {
      // Found a gcd, store the new values
      numerator /= gcd;
      denominator /= gcd;
    }
  }
  
  /**
   * Format rational number into String representation ( e.g. "1/4" )
   */
  public String toString()
  {
    // Check for a whole number (denominator is 1)
    if (denominator == 1)
      return "" + numerator; // Whole number, ignore the denominator output
    else
      return "" + numerator + "/" + denominator; // Rational number, print both
  }
  
  /**
   * Format rational number into floating point String representation
   * SEE given code on page 10
   */
  public String toDoubleString()
  {
    // Get formatting to two decimal places
    DecimalFormat twoDigits = new DecimalFormat(".00");
    // Return the formatted value
    return (twoDigits.format((double)numerator/(double)denominator));
  } 
}
